package com.yuanye.yeecho.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yuanye.yeecho.R;
import com.yuanye.yeecho.base.BaseFragment;

import java.util.List;

public class TaskListAdapter extends BaseAdapter {

    private Context context;
    private List<BaseFragment> testItems;

    public TaskListAdapter(Context context, List<BaseFragment> testItems) {
        this.context = context;
        this.testItems = testItems;
    }

    @Override
    public int getCount() {
        if (testItems == null) return 0;
        return testItems.size();
    }

    @Override
    public BaseFragment getItem(int i) {
        return testItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_task_list, null);
            holder.txtName = view.findViewById(R.id.test_name);
            holder.imgResult = view.findViewById(R.id.test_result);
            holder.progressBar = view.findViewById(R.id.test_pgb);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        holder.txtName.setText(testItems.get(i).getTaskName());
        int color;
        if (testItems.size() != 0){
            switch (testItems.get(i).getState()){
                case BaseFragment.PASS:
//                    color = context.getResources().getColor(R.color.colorPass);
//                    view.setBackgroundColor(color);
                    holder.progressBar.setVisibility(View.GONE);
                    holder.imgResult.setVisibility(View.VISIBLE);
                    holder.imgResult.setImageResource(R.drawable.success);
                    break;
                case BaseFragment.FAIL:
//                    color = context.getResources().getColor(R.color.colorFail);
//                    view.setBackgroundColor(color);
                    holder.progressBar.setVisibility(View.GONE);
                    holder.imgResult.setVisibility(View.VISIBLE);
                    holder.imgResult.setImageResource(R.drawable.fail);
                    break;
                case BaseFragment.TESTING:
                    holder.imgResult.setVisibility(View.GONE);
                    holder.progressBar.setVisibility(View.VISIBLE);
                    break;
                default:
                    holder.progressBar.setVisibility(View.GONE);
                    holder.imgResult.setVisibility(View.VISIBLE);
                    holder.imgResult.setImageResource(R.drawable.alert);
                    break;
            }

        }
        return view;
    }

    class ViewHolder{
        TextView txtName;
        ImageView imgResult;
        ProgressBar progressBar;
    }
}
